<?php
$path=$_SERVER["DOCUMENT_ROOT"]."/Clase/php/FORO/clases/";
include_once ($path."BaseDatos.php");

class Categoria{
	private $_nom;
	private $_conect;
	public function __construct(){}
	
	public function addCategoria(){
		$consulta="INSERT INTO `categoria` (nom, valid) VALUES ('".$_REQUEST["nom"]."', '1')";
		$conect = new BaseDatos();
        $e=$conect->prepare($consulta);
        $ex=$e->execute();
        if (!$ex) return "Error 900: No s'ha pogut anyadir la categoria.";
        header("Location: categories.php");
	}
	
	public function printCategories(){
		$consulta="select categoria.id AS id, categoria.nom AS nom, (select count(id) from tema where ".
		"tema.id_categoria=categoria.id and tema.valid='1') as unitats from categoria where valid = 1";
		$conect = new BaseDatos();
		$e = $conect->prepare($consulta);
		$ex=$e->execute();
		$r="";
		if (!$ex) return "Error 901: No hay categorias.";
		while($row=$e->fetch(PDO::FETCH_ASSOC)){
			$r.="<div class='resultat' onclick=\"goTo(".$row["id"].")\"><div class=\"resultatItem\">".
			$row["nom"]."</div><div class=\"resultatItem\" style=\"margin-left:40%;\">".$row["unitats"]."</div></div>";
		}
		return $r;
	}
	
	public function printCategoriesAdmin(){
		$consulta="select categoria.id AS id, categoria.nom AS nom, (select count(id) from tema where ".
				"tema.id_categoria=categoria.id and tema.valid='1') as unitats from categoria where categoria.valid = 1";
		$conect = new BaseDatos();
		$e = $conect->prepare($consulta);
		$ex=$e->execute();
		$r="";
		if (!$ex) return "Error 901: No hay categorias.";
		$row=$e->fetchALL(PDO::FETCH_ASSOC);
		return $row;
	}
	
	public function selectCategories(){
		$consulta="select categoria.id AS id, categoria.nom AS nom, (select count(id) from tema where ".
				"tema.id_categoria=categoria.id) as unitats from categoria where valid = 1";
		$conect = new BaseDatos();
		$e = $conect->prepare($consulta);
		$e->execute();
		$r="";
		if (!$e) return "Error 901: No hay categorias.";
		while($row=$e->fetch(PDO::FETCH_ASSOC)){
			$r.="<option value='".$row["id"]."'>".$row["nom"]."</option>";
		}
		return $r;
	}
	
	public function countCategories(){
		$consulta="Select COUNT(id) as unitats from categoria WHERE valid=1";
		$conect = new BaseDatos();
		$e = $conect->prepare($consulta);
		$e->execute();
		if (e==0) return "Error 901: No hay categorias.";
		$row=$e->fetchALL(PDO::FETCH_ASSOC);
		return $row["unitats"]; 
	}
	
	public function dropCategoria(){
		$consulta="UPDATE categoria SET valid='0' WHERE id='".$_REQUEST["drop"]."'";
		$conect = new BaseDatos();
		$e = $conect->prepare($consulta);
		$ex=$e->execute();
		if (!$ex) return "Error 902: No se ha eliminado la categoria.";
		else header("Location: categories.php");
	}
	
	public function editCategoria(){
		$consulta="UPDATE categoria SET nom='".$_REQUEST["nom"]."' WHERE id='".$_REQUEST["modify"]."'";
		$conect = new BaseDatos();
		$e = $conect->prepare($consulta);
		$ex=$e->execute();
		if ($ex) return "Error 903: No se ha modificado la categoria.";
		else header("Location: categories.php");
	}
	
	public function printCategoria($id){
		$consulta="Select nom from categoria where id=':id'";
		$conect = new BaseDatos();
		$sql = $conect->prepare($consulta);
		$sql->bindParam(':id', $id);
		$sql->execute();
		if (e==0) return "Error 904: No se ha encontrado la categoria.";
		while($row=$sql->fetch(PDO::FETCH_ASSOC))return $row["nom"];
	}
	
	public function getNom(){
		$consulta="Select nom from categoria where id='".$_REQUEST["edit"]."'";
		$conect = new BaseDatos();
		$sql = $conect->prepare($consulta);
		$e=$sql->execute();
		if (!$e) return "Error 904: No se ha encontrado la categoria.";
		while($row=$sql->fetch(PDO::FETCH_ASSOC))return $row["nom"];
	}
}

?>